/**
 * Toast提示工具类
 * 提供全局调用Toast组件的方法
 */

let toastInstance = null;

/**
 * 获取当前页面的Toast组件实例
 * @returns {Object|null} Toast组件实例
 */
const getToastInstance = () => {
  const pages = getCurrentPages();
  if (pages.length === 0) return null;
  
  const currentPage = pages[pages.length - 1];
  if (!currentPage) return null;
  
  if (toastInstance) return toastInstance;
  
  // 获取页面上的toast组件
  const instance = currentPage.selectComponent('#toast');
  if (!instance) {
    console.warn('未找到Toast组件，请确保页面中包含id为toast的Toast组件');
    return null;
  }
  
  toastInstance = instance;
  return instance;
};

/**
 * 重置Toast实例
 * 当页面切换时，需要重置Toast实例
 */
const resetToast = () => {
  toastInstance = null;
};

/**
 * 显示Toast提示
 * @param {Object|String} options 配置项或提示文字
 * @param {String} options.title 提示文字
 * @param {String} options.icon 图标类型，可选值：success, error, loading, none
 * @param {Number} options.duration 显示时长，单位毫秒，设为0则不自动关闭
 * @param {Boolean} options.mask 是否显示遮罩层
 * @returns {Promise} Promise对象，Toast关闭时resolve
 */
const showToast = (options) => {
  return new Promise((resolve) => {
    // 如果传入的是字符串，则认为是提示文字
    if (typeof options === 'string') {
      options = { title: options };
    }
    
    const instance = getToastInstance();
    if (!instance) {
      console.warn('未找到Toast组件，使用wx.showToast替代');
      
      // 使用系统toast兜底
      wx.showToast({
        title: options.title || '',
        icon: options.icon === 'success' ? 'success' : 'none',
        duration: options.duration || 2000,
        mask: options.mask || false
      });
      
      setTimeout(() => {
        resolve();
      }, options.duration || 2000);
      
      return;
    }
    
    // 关闭事件监听
    instance.unbind('close');
    instance.bind('close', () => {
      resolve();
    });
    
    // 设置toast内容并显示
    instance.setData({
      title: options.title || '',
      icon: options.icon || 'none',
      duration: options.duration !== undefined ? options.duration : 2000,
      mask: options.mask || false,
      show: true
    });
  });
};

/**
 * 隐藏Toast提示
 */
const hideToast = () => {
  const instance = getToastInstance();
  if (instance) {
    instance.hideToast();
  } else {
    wx.hideToast();
  }
};

/**
 * 显示加载中Toast
 * @param {String|Object} options 提示文字或配置项
 * @returns {Promise} Promise对象，Toast关闭时resolve
 */
const showLoading = (options) => {
  if (typeof options === 'string') {
    options = { title: options };
  }
  
  return showToast({
    title: options.title || '加载中...',
    icon: 'loading',
    duration: options.duration || 0, // 默认不自动关闭
    mask: options.mask !== undefined ? options.mask : true
  });
};

/**
 * 显示成功Toast
 * @param {String|Object} options 提示文字或配置项
 * @returns {Promise} Promise对象，Toast关闭时resolve
 */
const showSuccess = (options) => {
  if (typeof options === 'string') {
    options = { title: options };
  }
  
  return showToast({
    title: options.title || '成功',
    icon: 'success',
    duration: options.duration || 2000,
    mask: options.mask || false
  });
};

/**
 * 显示错误Toast
 * @param {String|Object} options 提示文字或配置项
 * @returns {Promise} Promise对象，Toast关闭时resolve
 */
const showError = (options) => {
  if (typeof options === 'string') {
    options = { title: options };
  }
  
  return showToast({
    title: options.title || '错误',
    icon: 'error',
    duration: options.duration || 2000,
    mask: options.mask || false
  });
};

module.exports = {
  showToast,
  hideToast,
  showLoading,
  showSuccess,
  showError,
  resetToast
}; 